# encoding:utf-8
import cv2
import module.utils.api as api
from module.utils.utils import image_to_base64
import json
import re

import requests
import base64


def main():
    # print(help(api))

    print(api.ocr_recog_one("特种设备使用登记证", image_to_base64(cv2.imread("flow_test/IMG_20210413_124404.jpg"))))
    print(api.ocr_recog_one("中华人民共和国道路运输证_半挂车", image_to_base64(cv2.imread("flow_test/IMG_20210413_124423.jpg"))))
    print(api.ocr_recog_one("中华人民共和国道路运输证_牵引车", image_to_base64(cv2.imread("flow_test/IMG_20210413_121707.jpg"))))
    print(api.ocr_recog_one("道路危险货物运输驾驶员证", image_to_base64(cv2.imread("flow_test/IMG_20210413_121624.jpg"))))
    print(api.ocr_recog_one("危险货物运输押运人员证", image_to_base64(cv2.imread("flow_test/IMG_20210413_121606.jpg"))))
    print(api.ocr_recog_one("中华人民共和国机动车驾驶证", image_to_base64(cv2.imread("flow_test/IMG_20210413_121646.jpg"))))
    print(api.ocr_recog_one("中华人民共和国机动车行驶证_半挂车", image_to_base64(cv2.imread("flow_test/IMG_20210413_121537.jpg"))))
    print(api.ocr_recog_one("中华人民共和国机动车行驶证_牵引车", image_to_base64(cv2.imread("flow_test/IMG_20210413_121524.jpg"))))

    # print(api.car_plate_extract(image_to_base64(cv2.imread("flow_test_2/IMG_20210413_133859.jpg"))))

    # print(api.ocr_recog_one("特种设备使用登记证", image_to_base64(cv2.imread("flow_test_2/IMG_20210413_134332.jpg"))))
    # print(api.ocr_recog_one("中华人民共和国道路运输证_半挂车", image_to_base64(cv2.imread("flow_test_2/IMG_20210413_134318.jpg"))))
    # print(api.ocr_recog_one("中华人民共和国道路运输证_牵引车", image_to_base64(cv2.imread("flow_test_2/IMG_20210413_134252.jpg"))))
    # print(api.ocr_recog_one("道路危险货物运输驾驶员证", image_to_base64(cv2.imread("flow_test_2/IMG_20210413_134143.jpg"))))
    # print(api.ocr_recog_one("危险货物运输押运人员证", image_to_base64(cv2.imread("flow_test_2/IMG_20210413_134215.jpg"))))
    # # print(api.ocr_recog_one("中华人民共和国机动车驾驶证", image_to_base64(cv2.imread(""))))
    # print(api.ocr_recog_one("中华人民共和国机动车行驶证_半挂车", image_to_base64(cv2.imread("flow_test_2/IMG_20210413_134202.jpg"))))
    # print(api.ocr_recog_one("中华人民共和国机动车行驶证_牵引车", image_to_base64(cv2.imread("flow_test_2/IMG_20210413_134239.jpg"))))

    # print(api.ocr_recog_one("特种设备使用登记证", image_to_base64(cv2.imread("flow_test_3/IMG_20210414_113901.jpg"))))
    # print(api.ocr_recog_one("中华人民共和国道路运输证_半挂车", image_to_base64(cv2.imread("flow_test_3/IMG_20210414_113736.jpg"))))
    # print(api.ocr_recog_one("中华人民共和国道路运输证_牵引车", image_to_base64(cv2.imread("flow_test_3/IMG_20210414_114002.jpg"))))
    # print(api.ocr_recog_one("道路危险货物运输驾驶员证", image_to_base64(cv2.imread("flow_test_3/IMG_20210414_113815.jpg"))))
    # print(api.ocr_recog_one("危险货物运输押运人员证", image_to_base64(cv2.imread("flow_test_3/IMG_20210414_113832.jpg"))))
    # print(api.ocr_recog_one("中华人民共和国机动车驾驶证", image_to_base64(cv2.imread("flow_test_3/IMG_20210414_113610.jpg"))))
    # print(api.ocr_recog_one("中华人民共和国机动车行驶证_半挂车", image_to_base64(cv2.imread("flow_test_3/IMG_20210414_113659.jpg"))))
    # print(api.ocr_recog_one("中华人民共和国机动车行驶证_牵引车", image_to_base64(cv2.imread("flow_test_3/IMG_20210414_113715.jpg"))))

    # print(api.car_plate_extract(image_to_base64(cv2.imread("flow_test_3/IMG_20210414_122219.jpg"))))
    # print(api.car_plate_extract(image_to_base64(cv2.imread("flow_test_3/IMG_20210414_122233.jpg"))))

    # print(api.ocr_recog_one("特种设备使用登记证", image_to_base64(cv2.imread("test/7.jpg"))))
    # print(api.ocr_recog_one("中华人民共和国道路运输证_半挂车", image_to_base64(cv2.imread("test/12.png"))))
    # print(api.ocr_recog_one("中华人民共和国道路运输证_牵引车", image_to_base64(cv2.imread("test/16.jpg"))))
    # print(api.ocr_recog_one("道路危险货物运输驾驶员证", image_to_base64(cv2.imread("test/10.jpg"))))
    # print(api.ocr_recog_one("危险货物运输押运人员证", image_to_base64(cv2.imread("test/1.jpg"))))
    # print(api.ocr_recog_one("中华人民共和国机动车驾驶证", image_to_base64(cv2.imread("test/8.jpg"))))
    # print(api.ocr_recog_one("中华人民共和国机动车行驶证_半挂车", image_to_base64(cv2.imread("test/14.jpg"))))
    # print(api.ocr_recog_one("中华人民共和国机动车行驶证_牵引车", image_to_base64(cv2.imread("test/3.jpg"))))

    # data = api.face_register(image_to_base64(cv2.imread("test/8.jpg")))
    # feature = json.loads(data)["feature"][0]
    # print(api.match_face(image_to_base64(cv2.imread("test/face.png")), feature))


def access():
    # client_id 为官网获取的AK， client_secret 为官网获取的SK
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=SeQC3YWoGMkaVuIV4W6zmwX8&client_secret=0LfV7L5Fku33qCkWm6LbCyvn7v2eeSdz'
    response = requests.get(host)
    if response:
        print(response.json())


def driving():
    """
    驾驶证识别
    {'words_result': {'姓名': {'words': '方海桥'}, '至': {'words': '20220104'}, '出生日期': {'words': '19700325'},
    '证号': {'words': '37078619700325633X'}, '住址': {'words': '山东省昌邑市丈岭镇麻姑庄村205号'},
    '初次领证日期': {'words': '19940104'}, '国籍': {'words': '中国'}, '准驾车型': {'words': 'A2'},
    '性别': {'words': '男'}, '有效期限': {'words': '20160104'}}, 'log_id': 1390947863130800128,
    'words_result_num': 10, 'direction': -1}

    {'words_result': {'姓名': {'words': '黄国良'}, '有效起始日期': {'words': '20111128'}, '出生日期': {'words': '19690911'},
    '证号': {'words': '370634196909110218'}, '住址': {'words': '山东省烟台市长岛县北长山乡北城村37号'},
    '初次领证日期': {'words': '19911128'}, '国籍': {'words': '中国'}, '准驾车型': {'words': 'A2'},
    '性别': {'words': '男'}, '有效期限': {'words': '10年'}}, 'log_id': 1390955110871334912,
    'words_result_num': 10, 'direction': -1}
    """
    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license"
    # 二进制方式打开图片文件
    f = open('flow_test/1620367045115.jpg', 'rb')
    img = base64.b64encode(f.read())
    params = {"image": img}
    access_token = '24.8ac99f1a5568971ef38e75f915c33dfe.2592000.1623051248.282335-24121783'
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        print(response.json())
        result = response.json()
        print(result['words_result']['姓名']['words'])


def vehicle():
    """
    行驶证识别
    {'words_result': {'车辆识别代号': {'words': 'L584C4N99LD004724'},
    '住址': {'words': '山东省烟台市龙口市龙口经济开发区河物流股份有限公司综合物流园'},
    '发证单位': {'words': '山东省烟台市公安局交通警察支队'}, '发证日期': {'words': '20200716'},
    '品牌型号': {'words': '集瑞联合牌QCC4253N664W'}, '车辆类型': {'words': '重型半挂牵引车'},
    '所有人': {'words': '山东优化物流有限公司'}, '使用性质': {'words': '非营运'},
    '发动机号码': {'words': '3120F057781'}, '号牌号码': {'words': '鲁FBR932'}, '注册日期': {'words': '20200716'}},
    'log_id': 1390946564205182976, 'words_result_num': 11}
    """
    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license"
    # 二进制方式打开图片文件
    f = open('test/3.jpg', 'rb')
    img = base64.b64encode(f.read())
    params = {"image": img}
    access_token = '24.8ac99f1a5568971ef38e75f915c33dfe.2592000.1623051248.282335-24121783'
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        print(response.json())
        result = response.json()
        print(result['words_result']['号牌号码']['words'])


def process_driveing(response):
    result = {"姓名": response['words_result']['姓名']['words'],
              "证号": response['words_result']['证号']['words'],
              "准驾车型": response['words_result']['准驾车型']['words']}
    if "至" in response['words_result'].keys():
        result["有效期"] = response['words_result']['至']['words'][:4] + '-' + response['words_result']['至']['words'][
                                                                           4:6] + '-' + \
                        response['words_result']['至']['words'][6:]
    elif "有效起始日期" in response['words_result'].keys() and "有效期限" in response['words_result'].keys():
        year = int(response['words_result']['有效起始日期']['words'][:4]) + \
               int(re.search("[0-9]+", response['words_result']['有效期限']['words']).group(0))
        result["有效期"] = str(year) + '-' + response['words_result']['有效起始日期']['words'][4:6] + '-' + \
                        response['words_result']['有效起始日期']['words'][6:]
    return result


def process_vehicle(response):
    result = {"有效期": None, "车牌": response['words_result']['号牌号码']['words'],
              "使用性质": response['words_result']['使用性质']['words']}
    return result



if __name__ == '__main__':
    main()
    # dic1 = {'words_result': {'姓名': {'words': '黄国良'}, '有效起始日期': {'words': '20111128'},
    #                          '出生日期': {'words': '19690911'}, '证号': {'words': '370634196909110218'},
    #                          '住址': {'words': '山东省烟台市长岛县北长山乡北城村37号'}, '初次领证日期': {'words': '19911128'},
    #                          '国籍': {'words': '中国'}, '准驾车型': {'words': 'A2'}, '性别': {'words': '男'},
    #                          '有效期限': {'words': '10年'}}, 'log_id': 1390955110871334912, 'words_result_num': 10,
    #         'direction': -1}
    #
    # dic2 = {'words_result': {'姓名': {'words': '方海桥'}, '至': {'words': '20220104'}, '出生日期': {'words': '19700325'},
    #                          '证号': {'words': '37078619700325633X'}, '住址': {'words': '山东省昌邑市丈岭镇麻姑庄村205号'},
    #                          '初次领证日期': {'words': '19940104'}, '国籍': {'words': '中国'}, '准驾车型': {'words': 'A2'},
    #                          '性别': {'words': '男'}, '有效期限': {'words': '20160104'}}, 'log_id': 1390947863130800128,
    #         'words_result_num': 10, 'direction': -1}
    # dic3 = {'words_result': {'车辆识别代号': {'words': 'L584C4N99LD004724'},
    #                          '住址': {'words': '山东省烟台市龙口市龙口经济开发区河物流股份有限公司综合物流园'},
    #                          '发证单位': {'words': '山东省烟台市公安局交通警察支队'}, '发证日期': {'words': '20200716'},
    #                          '品牌型号': {'words': '集瑞联合牌QCC4253N664W'}, '车辆类型': {'words': '重型半挂牵引车'},
    #                          '所有人': {'words': '山东优化物流有限公司'}, '使用性质': {'words': '非营运'},
    #                          '发动机号码': {'words': '3120F057781'}, '号牌号码': {'words': '鲁FBR932'},
    #                          '注册日期': {'words': '20200716'}},
    #         'log_id': 1390946564205182976, 'words_result_num': 11}
    # print(process_vehicle(dic3))
